module ApplicationHelper
  def options_for_per_page
    [10, 16, 25, 50, 100]
  end

  # 方法
  def per_page_select
    render partial: '/share/per_page_num_selector'
  end

  # 搜索表单的按钮
  def search_button(f)
    render partial: '/share/search_button', locals: { f: f }
  end

  def html_buttons(&block)
    content_for :html_buttons, &block
  end

  # 清空按钮
  def reset_button(btn_size = 'xs')
    link_to t(:empty), 'javascript:void(0)', class: "btn btn-#{btn_size} btn-info push-5-l", type: 'reset'
  end

  def close_modal_button
    link_to t(:close), 'javascript:void(0)', class: 'btn btn-default btn-md push-20-l', data: { dismiss: 'modal' }
  end

  def truncate_str(str, pop_str = nil, length = 20)
    link_to truncate(str, length: length), 'javascript:void(0);',
            'data' => { 'toggle' => 'tooltip', 'placement' => 'right', 'html' => 'true',
                        'original-title' => simple_format(pop_str || str) }
  end

  # bootstrap的modal弹框，统一用的一个div#application-modal，每次modal关闭的时候会清空内容
  def link_to_dialog(name, url, **args)
    args[:data] ||= {}
    args[:data].merge!(toggle: 'modal', target: '#application-modal')
    link_to name, url, args
  end

  # 中型号的弹窗
  def link_to_md_dialog(name, url, **args)
    args[:data] ||= {}
    args[:data].merge!(toggle: 'modal', target: '#application-md-modal')
    link_to name, url, args
  end

  # 小型号的弹窗
  def link_to_sm_dialog(name, url, **args)
    args[:data] ||= {}
    args[:data].merge!(toggle: 'modal', target: '#application-sm-modal')
    link_to name, url, args
  end

  def ts(*arr)
    j = (I18n.locale.to_s == 'en' ? ' ' : '')
    arr.flatten.map { |a| a.is_a?(Class) ? a.model_name.human : t(a) }.join(j)
  end

  def boolean_icon(b)
    return '' if b.to_s.blank?
    (b ? "<i class='fa fa-check text-success'></i>" : "<i class='fa fa-close text-danger'></i>").html_safe
  end
end
